﻿using NetTaste;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using XjjXmm.Infrastructure.Exceptions;
using XjjXmm.Infrastructure.ToolKit;

namespace xjjxmm.infrastructure.ToolKit
{
    public static class AttributeKit
    {
        public static T? GetObjectCustomAttribute<T>(this object o) where T : Attribute
        {
            return o.GetType().GetCustomAttribute<T>();
        }

        public static PropertyInfo? GetPropertyInfoHaveCustomAttribute<T>(this object obj) where T : Attribute
        {
            var type = TypeKit.GetType(obj.GetType());
            return type.GetProperties().FirstOrDefault(t => t.GetCustomAttribute<T>() != null);
        }

        public static object? GetValueHaveCustomAttribute<T>(this object obj) where T : Attribute
        {
            var type = TypeKit.GetType(obj.GetType());
            var propertyInfo = type.GetProperties().FirstOrDefault(t => t.GetCustomAttribute<T>() != null);
            
            return propertyInfo?.GetValue(obj);
        }
    }
}
